package com.zzyl.common.utils.readpdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

/**
 * 读取PDF文件工具类
 *
 * @Author mr.luo
 * @Date 22 5月 2025 00:43
 */
@Component
public class PdfUtil {
    public  String readPdf(InputStream inputStream) {
        PDDocument pdfDocument = null;

        try {
            pdfDocument = PDDocument.load(inputStream);
            PDFTextStripper pdfTextStripper = new PDFTextStripper();
            String text = pdfTextStripper.getText(pdfDocument);
            return text;
            // 获取PDF文档
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if ( pdfDocument != null) {
                try {
                    inputStream.close();
                    pdfDocument.close();
                } catch (IOException e) {
                   e.printStackTrace();
                }
            }
        }
        return null;
    }
}
